import { _decorator, Component, Label, Node, ProgressBar, Sprite, SpriteFrame } from 'cc';
import { AWARYTYPE, LV, treasureBoxAward, treasureBoxTarget } from '../AshFramework/Datas/GameConstant';
import { GameManager } from './GameManager';
import { GameUtil } from './GameUtil';
import { MenuManager } from './MenuManager';
const { ccclass, property } = _decorator;

@ccclass('TreasureBox')
export class TreasureBox extends Component {
    @property
    Num = 0;

    @property(Sprite)
    Sprite: Sprite = null;

    @property(SpriteFrame)
    normalSF: SpriteFrame = null;

    @property(SpriteFrame)
    CanGetSF: SpriteFrame = null;

    @property(SpriteFrame)
    CancelSF: SpriteFrame = null;

    @property(Label)
    ConditionLabel: Label = null;

    private _progressBar: ProgressBar = null;

    private _epoch;
    private _lv;
    private _targetNum: number = 0;
    private _awardNum: number = 0;
    private _isClick: boolean = false;
    private isGet: boolean = false;

    init() {
        this._progressBar = this.node.parent.getChildByName("宝箱进度条底部").getComponent(ProgressBar);
        this._epoch = GameManager.Instance.userData.epoch;
        this._lv = GameManager.Instance.userData.lv;
        this._targetNum = treasureBoxTarget[this._epoch][this._lv][this.Num - 1];
        this._awardNum = treasureBoxAward[this._epoch][this._lv][this.Num - 1];
        this.ConditionLabel.string = String(this._targetNum);

        this.showTreasureBox();
    }

    showTreasureBox() {
        if (GameManager.Instance.userData.treasureBoxAward[this._epoch][this._lv][this.Num]) {
            this.Sprite.spriteFrame = this.CancelSF;
            this.isGet = true;
            this._isClick = true;
            if (this.Num / 2 > this._progressBar.progress) {
                this._progressBar.progress = this.Num / 2;
            }
        } else if (GameManager.Instance.userData.wipeOut[this._epoch][this._lv] >= this._targetNum) {
            this.Sprite.spriteFrame = this.CanGetSF;
            this.isGet = false;
            this._isClick = true;
            if (this.Num / 2 > this._progressBar.progress) {
                this._progressBar.progress = this.Num / 2;
            }
        } else {
            this.Sprite.spriteFrame = this.normalSF;
            this.isGet = false;
            this._isClick = false;
        }
        console.log(GameManager.Instance.userData.wipeOut[this._epoch][this._lv]);

    }

    click() {
        if (this.isGet) {
            //玩家已领取
            GameUtil.showHint(`奖励已领取`)
            return;
        } else if (!this._isClick) {
            //未达到条件
            GameUtil.showHint(`未达到领取条件`)
            return;
        }
        //领取-------------
        GameUtil.getAward(AWARYTYPE.MASONRY, MenuManager.Instance.getMasonryWorldPos(), () => {
            GameManager.Instance.userData.treasureBoxAward[this._epoch][this._lv][this.Num] = true;
            MenuManager.Instance.addMasonry(this._awardNum);
            this.showTreasureBox();
        })
    }

}


